
{***********************************************************}
{                                                           }
{      Luxena Database Library                              }
{                                                           }
{      TLXUpdateSQL component                               }
{                                                           }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit LXUpdateSQL;

{$I lx.inc}

interface

uses
  Classes, DB, LXDataSetImpl;

type

{ TLXUpdateSQL }

  TLXUpdateSQL = class(TLXDataSetUpdateObject)
  private
    FSQLText: array [0..3] of TStrings;
    procedure SetSQLUpdateKind(UpdateKind: TUpdateKind; Value: TStrings);
    function GetSQLUpdateKind(UpdateKind: TUpdateKind): TStrings;
    procedure SetSQLIndex(Index: Integer; Value: TStrings);
    function GetSQLIndex(Index: Integer): TStrings;
  protected
    function Apply(Delta: TDataSet; UpdateKind: TUpdateKind; var RowsAffected: Integer): Boolean; override;
    function Refresh(Delta: TDataSet; UpdateKind: TUpdateKind): Boolean; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property SQL[UpdateKind: TUpdateKind]: TStrings read GetSQLUpdateKind write SetSQLUpdateKind;
  published
    property ModifySQL: TStrings index 0 read GetSQLIndex write SetSQLIndex;
    property InsertSQL: TStrings index 1 read GetSQLIndex write SetSQLIndex;
    property DeleteSQL: TStrings index 2 read GetSQLIndex write SetSQLIndex;
    property RefreshSQL: TStrings index 3 read GetSQLIndex write SetSQLIndex;
  end;

  
implementation


{ TLXUpdateSQL }

constructor TLXUpdateSQL.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSQLText[0] := TStringList.Create;
  FSQLText[1] := TStringList.Create;
  FSQLText[2] := TStringList.Create;
  FSQLText[3] := TStringList.Create;
end;

destructor TLXUpdateSQL.Destroy;
begin
  FSQLText[0].Free;
  FSQLText[1].Free;
  FSQLText[2].Free;
  FSQLText[3].Free;
  inherited Destroy;
end;

function TLXUpdateSQL.Apply(Delta: TDataSet; UpdateKind: TUpdateKind;
  var RowsAffected: Integer): Boolean;
begin
  Result := SQL[UpdateKind].Text <> '';
  if Result then
    RowsAffected := DataSetImpl.UpdateSQLSupport.ModifyRecord(Delta, UpdateKind,
      SQL[UpdateKind].Text);
end;

function TLXUpdateSQL.Refresh(Delta: TDataSet; UpdateKind: TUpdateKind): Boolean;
begin
  Result := RefreshSQL.Text <> '';
  if Result then
    DataSetImpl.UpdateSQLSupport.RefreshRecord(Delta, UpdateKind, RefreshSQL.Text);
end;

procedure TLXUpdateSQL.SetSQLUpdateKind(UpdateKind: TUpdateKind; Value: TStrings);
begin
  SetSQLIndex(Ord(UpdateKind), Value);
end;

function TLXUpdateSQL.GetSQLUpdateKind(UpdateKind: TUpdateKind): TStrings;
begin
  Result := GetSQLIndex(Ord(UpdateKind));
end;

procedure TLXUpdateSQL.SetSQLIndex(Index: Integer; Value: TStrings);
begin
  FSQLText[Index].Assign(Value);
end;

function TLXUpdateSQL.GetSQLIndex(Index: Integer): TStrings;
begin
  Result := FSQLText[Index];
end;


end.